<html>
	<head>
		<script type="text/javascript" src="jsunit/app/jsUnitCore.js"></script>
		<script type="text/javascript" src="../lib/jquery-1.2.6.js"></script>
		<script type="text/javascript" src="../src/jquery.jhandle.js"></script>
		<script type="text/javascript">
		
		    function assertFunction(f)
		    {
		    
		        // make sure what they say actually exists
		        assertNotUndefined("Function/Method expected but is undefined: " + f, eval(f));
		        assertNotNull("Function/Method expected but is null: " + f, eval(f));
		        
		        // make sure it is a function
		        if (!$.isFunction(eval(f)))
		            fail(f + " expected to be a 'function' but is '" + typeof(eval(f)) + "'.");
		            
		    }
		
		    // general counter used for testing
		    var globalCounter = 0;
		    
		    function setUp()
		    {
		        // reset the counter
		        globalCounter = 0;
		        // clear the handlers cache
		        $.__handlers = {};
		    }
		
			// Test to make sure the methods and functions were created
			// This will make sure the normal plumbing of the plugin is working	
			function testInternals()
			{
				
				assertNotUndefined("$.__handlers array was not defined.", $.__handlers);

			}

            // test to ensure the handler function gets the correct options
            function test_handlerGetsOptions()
            {
                
                $.handle("a.confirm", function(options){
					
					assertNotUndefined("The options passed to the handler function is undefined.", options);
					
					if (options.index != globalCounter)
					    fail("The index is not passed in the options to the handling function");
					    
                    globalCounter++;
					
				});
                
            }

			// tests to make sure the context ('this') IS the element being handled
			function test_contextOfHandlerIsElement()
			{
				
				$.handle("a.confirm", function(options){
					
					// make sure the context (this) is the first link
					assertObjectEquals($("a.confirm")[options.index], this);
					
				});

			}
			
			function test_handle()
			{
			    assertFunction("$.handle");
			    //assertNotUndefined("$.handle() function was not defined.", $.handle);
			}

			// tests to make sure the handler is called initially when the handle method is called
			// - tests to make sure the function is called 
			// - tests to make sure the function is called ONCE only
			function test_initiallyCallsHandlers()
			{
				
				var handlerCallCount = 0;
				$.handle("a.confirm", function(){
					handlerCallCount++;
				});
				
				if (handlerCallCount == 0)
					fail(".handle(selector, function) - function was not called initially")
				
				assertEquals("The handler should have been called 3 times, but it was called " + handlerCallCount + " times.", 3, handlerCallCount)
				
			}
			
			function test_handleArguments()
			{

		        var handler = function(){ return 1; };
		        var unhandler = function(){ return 2; };
		        var options = { value: 3 };
		        
		        // call all four possible handler siguatures
			    $.handle(".one", handler);
			    $.handle(".two", handler, unhandler);
			    $.handle(".three", handler, unhandler, options);
			    $.handle(".four", handler, options);
			
			    // make sure all handlers we set correctly
			    assertEquals(handler, $.__handlers[".one"][0].handler);
			    assertEquals(handler, $.__handlers[".two"][0].handler);
			    assertEquals(handler, $.__handlers[".three"][0].handler);
			    assertEquals(handler, $.__handlers[".four"][0].handler);
			
			    // one and four SHOULDN'T have unhandlers
			    assertUndefined("No unhandler was passed to $.handle() but one was present.", $.__handlers[".one"][0].unhandler);
			    assertUndefined("No unhandler was passed to $.handle() but one was present.", $.__handlers[".four"][0].unhandler);
			    
			    // two and three should have unhandlers
			    assertNotUndefined("An unhandler was passed to $.handle() but is undefined", $.__handlers[".two"][0].unhandler);
			    assertNotUndefined("An unhandler was passed to $.handle() but is undefined", $.__handlers[".three"][0].unhandler);
                assertEquals("The unhandler that was passed to $.handle() was not the same one saved in the cache.", unhandler, $.__handlers[".two"][0].unhandler);
			    assertEquals("The unhandler that was passed to $.handle() was not the same one saved in the cache.", unhandler, $.__handlers[".three"][0].unhandler);
			
			    // make sure the options were merged on three and four
			    assertNotUndefined("Options were passed to $.handle() but were not merged", $.__handlers[".three"][0].value);
			    assertNotUndefined("Options were passed to $.handle() but were not merged", $.__handlers[".four"][0].value);
			    assertEquals("Options were passed to $.handle() but were not correctly merged", 3, $.__handlers[".three"][0].value);
			    assertEquals("Options were passed to $.handle() but were not correctly merged", 3, $.__handlers[".four"][0].value);
			    
			}
		
		    // tests to make sure that the internal __handlers object keeps a
		    // cache of the handler specified.
		    function test_handlersAreCached()
		    {
		    
		        var selector = "a.confirm";
		        var handler = function(){ return 1; };

				$.handle(selector, handler);
				
				assertNotUndefined("$.__handlers[selector] expected not to be undefined.", $.__handlers[selector]);
		        assertEquals("$.__handlers[selector] array expected to have a length of 1.", 1, $.__handlers[selector].length);
		        assertEquals("$.__handlers[selector][0] expected to match the handler passed via $.handle().", handler, $.__handlers[selector][0].handler);
		    
		    }
		    
		    // test to make sure that calling handle() on a specific element (or set of elements)
		    // searches through the currently specified handlers and 'handles' any elements that have
		    // not yet been handled.
		    function test_handleSpecificElements()
		    {
		    
                assertFunction("$('*').handle");
		   
		        var selector = "a.confirm";
		        var handler = function(){ globalCounter++; };
		        
		        // handle the three main elements
		        $.handle(selector, handler);
		        
		        // TEST: make sure the handler was called three times
		        assertEquals("counter expected to be 3 but was " + globalCounter, 3, globalCounter);
		        
		        // reset the counter
		        globalCounter = 0;
		        
		        // add another element
		        $("#links").append("<li><a id='fourth_link' href='four.html' class='confirm'>Four</a></li>").handle();

		        // clean up after this test
		        $("#fourth_link").remove();
		        
		        // TEST: make sure the handler was called one more time
		        assertEquals("The handler method for the new element was expected to be called once.  It was called " + globalCounter + " times.", 1, globalCounter);
		    
		    }

/*		    
		    function test_removeHandler()
		    {
		    
		        // make sure we have a removeHandler method
		        assertNotUndefined("$.removeHandler", $.removeHandler);
		    
		        var selector = "a.confirm";
		        var handler = function(){ return 1; };
		        
		        $.handle(selector, handler);
		        
		        assertNotUndefined("$.__handlers[selector]", $.__handlers[selector]);

		    }
*/

		</script>
	</head>
	<body>
		<ul id="links">
			<li><a id="first_link" href="one.html" class="confirm">One</a></li>
			<li><a id="second_link" href="two.html" class="confirm">Two</a></li>
			<li><a id="third_link" href="three.html" class="confirm">Three</a></li>
		</ul>
	</body>
</html>